package com.sign2024.sign2024core.common.utils;

import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;

/**
 * excel 样式工具类
 *
 * @author yujie
 * @data 2024/8/14 15:10
 */
public class ExcelStyleUtils {

    /**
     * excel 样式 有边框 （允许自动换行）
     * 表头以及内容 字体大小 10 样式 宋体
     *
     * @return
     */
    public static HorizontalCellStyleStrategy createStyleBorder() {
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景设置为红色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE1.getIndex());
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 10);
        // 设置字体加粗
        headWriteFont.setBold(true);
        headWriteFont.setFontName("宋体");
        headWriteCellStyle.setWriteFont(headWriteFont);
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);


        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 背景无色
        contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE1.getIndex());
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
        contentWriteFont.setFontHeightInPoints((short) 10);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        contentWriteFont.setFontName("宋体");

        // 设置边框颜色
//        contentWriteCellStyle.setTopBorderColor(IndexedColors.BLACK1.getIndex());
//        contentWriteCellStyle.setBottomBorderColor(IndexedColors.BLACK1.getIndex());
//        contentWriteCellStyle.setLeftBorderColor(IndexedColors.BLACK1.getIndex());
//        contentWriteCellStyle.setRightBorderColor(IndexedColors.BLACK1.getIndex());

//        contentWriteCellStyle.setTopBorderColor(IndexedColors.RED.getIndex());
//        contentWriteCellStyle.setBottomBorderColor(IndexedColors.RED.getIndex());
//        contentWriteCellStyle.setLeftBorderColor(IndexedColors.RED.getIndex());
//        contentWriteCellStyle.setRightBorderColor(IndexedColors.RED.getIndex());

        // 边框样式 左 右 上 下
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);


        // 设置字体居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        // 允许自动换行
        contentWriteCellStyle.setWrapped(true);

        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);


        return horizontalCellStyleStrategy;
    }

    /**
     * excel 样式 有边框（允许自动换行）
     *
     * @param topFontPoint 表头字体大小
     * @param topFontName  表头字体样式
     * @param fontPoint    内容字体大小
     * @param fontName     内容字体样式
     * @return
     */
    public static HorizontalCellStyleStrategy createStyleBorder(short topFontPoint, String topFontName,
                                                                short fontPoint, String fontName) {
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景设置为红色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE1.getIndex());
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 10);
        // 设置字体加粗
        headWriteFont.setBold(true);
        headWriteFont.setFontName("宋体");
        headWriteCellStyle.setWriteFont(headWriteFont);
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);


        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 背景无色
        contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE1.getIndex());
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
        contentWriteFont.setFontHeightInPoints((short) 10);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        contentWriteFont.setFontName("宋体");

        // 设置边框颜色
//        contentWriteCellStyle.setTopBorderColor(IndexedColors.BLACK1.getIndex());
//        contentWriteCellStyle.setBottomBorderColor(IndexedColors.BLACK1.getIndex());
//        contentWriteCellStyle.setLeftBorderColor(IndexedColors.BLACK1.getIndex());
//        contentWriteCellStyle.setRightBorderColor(IndexedColors.BLACK1.getIndex());

//        contentWriteCellStyle.setTopBorderColor(IndexedColors.RED.getIndex());
//        contentWriteCellStyle.setBottomBorderColor(IndexedColors.RED.getIndex());
//        contentWriteCellStyle.setLeftBorderColor(IndexedColors.RED.getIndex());
//        contentWriteCellStyle.setRightBorderColor(IndexedColors.RED.getIndex());

        // 边框样式 左 右 上 下
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);


        // 设置字体居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        // 允许自动换行
        contentWriteCellStyle.setWrapped(true);

        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

        return horizontalCellStyleStrategy;
    }

}
